/*
 * test.java
 *
 * Created on 10. Juli 2011, 15:55
 */
package de.kundepro.example.vaadin;

import de.kundepro.example.vaadin.entities.FirmaRechtsformen;
import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ConversionException;
import com.vaadin.data.Property.ReadOnlyException;
import com.vaadin.ui.*;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Table;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.CloseEvent;
import java.util.ArrayList;
import java.util.Collection;

/** 
 *
 * @author Andreas
 * @version 
 */
public class test extends Application implements
        TabSheet.SelectedTabChangeListener, Property.ValueChangeListener {

    final Table tbCompanies = new Table("Firmen");
    final Table tbPeople = new Table("Personen");
//    HashSet<Object> markedRows = new HashSet<Object>();
//    static final Action ACTION_MARK = new Action("Mark");
//    static final Action ACTION_UNMARK = new Action("Unmark");
//    static final Action ACTION_LOG = new Action("Save");
//    static final Action[] ACTIONS_UNMARKED = new Action[]{ACTION_MARK,
//        ACTION_LOG};
//    static final Action[] ACTIONS_MARKED = new Action[]{ACTION_UNMARK,
//        ACTION_LOG};
    Container container;
    private TabSheet t;
//    private TabSheet tab_firm;
    Window mainWindow;
    Window subWinCompDetail;
    Window subWinPersDetail;
    TextField searchCo;
    TextField searchPe;
    ArrayList<Firma> firmen;
    ArrayList<Person> personen;
    String suchbegriff;
    String datensatz;
    Collection col;
    DataAccessObject dao;
    private static final IndexedContainer firm_container = new IndexedContainer();
    private static final IndexedContainer pers_container = new IndexedContainer();
    final VerticalLayout l1 = new VerticalLayout();
    final VerticalLayout l2 = new VerticalLayout();
    final VerticalLayout l3 = new VerticalLayout();
    String value = "";
    ArrayList<Firma> company = new ArrayList<Firma>();
    ArrayList<FirmaRechtsformen> companyRecht = new ArrayList<FirmaRechtsformen>();
    ArrayList<FirmaEinheiten> companyDetails = new ArrayList<FirmaEinheiten>();
    boolean anzeigen = false;
    Label ausgabe;
    classFirmenDetails cfd = new classFirmenDetails();
    CompanyDetail cd;

    @Override
    public void init() {

        dao = new DataAccessObject();

        mainWindow = new Window("KundePro");
        mainWindow.setSizeFull();
//        subWinCompDetail = new Window("Firmen-Details");

        /* ArrayList wird mit den Firmen-Datensätzen befüllt*/
        firmen = dao.getAllFirmen();
        personen = dao.getAllPersonen();



        /* Überschriften der Tabelle Firmen*/
        firm_container.addContainerProperty("Id",
                String.class, null);
        firm_container.addContainerProperty("Firma",
                String.class, null);


        /* Überschriften der Tabelle Personen*/
        pers_container.addContainerProperty("Id",
                String.class, null);
        pers_container.addContainerProperty("Name",
                String.class, null);
        pers_container.addContainerProperty("Vorname",
                String.class, null);
        pers_container.addContainerProperty("Strasse",
                String.class, null);
        pers_container.addContainerProperty("PLZ",
                String.class, null);
        pers_container.addContainerProperty("Ort",
                String.class, null);

        for (int i = 0; i < firmen.size(); i++) {
            fillContainerCompanies(i);
        }

        for (int i = 0; i < personen.size(); i++) {
            fillContainerPeople(i);
        }


        /* Container an die Tabelle übergeben */
        tbCompanies.setContainerDataSource(firm_container);
        tbPeople.setContainerDataSource(pers_container);


        // Reiter / Tab 1
        l1.setMargin(true);
        // Reiter / Tab 2 
        l2.setMargin(true);
        // Reiter / Tab 3 
        l3.setMargin(true);


        // Tab-Sheet (Reiter-Menü)
        t = new TabSheet();
        t.setSizeFull();
//        t.setHeight("900px");
//        t.setWidth("1200px");
        t.addTab(l1, "Firmen", null);
        t.addTab(l2, "Personen", null);
        t.addTab(l3, "Info", null);

        t.addListener(this);


        mainWindow.addComponent(t);

        setMainWindow(mainWindow);



//        /* Zeigt die ausgewählten Zeilen/Datensätze an */
//        final Label selected = new Label("No selection");
//        l1.addComponent(selected);


        final Button detOeffnen = new Button("Firmendetails");

        detOeffnen.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {

                if (!value.isEmpty()) {

                    company = dao.getCompany(value);
//                    companyRecht = dao.getRechtsform(value);
                    companyDetails = dao.getAllCompanyDetails(value);


                    cfd.setFirmaEinheiten(companyDetails);
                    cfd.setFirma(company);

                    cd = new CompanyDetail(cfd);
                    subWinCompDetail = cd.oeffneFirmenDetails();
                    mainWindow.addWindow(subWinCompDetail);
                    subWinCompDetail.addListener(new Window.CloseListener() {

                        public void windowClose(CloseEvent e) {
//                            FirmaEinheiten update = new FirmaEinheiten();
//                            dao.setCompanyDetails(cd.getStandortDaten());
                            mainWindow.removeWindow(subWinCompDetail);

                        }
                    });
                } else if (value.matches("")) {

                    mainWindow.showNotification("Bitte Firma wählen");

                }


            }
        });




        l1.addComponent(detOeffnen);
        // size
        tbCompanies.setWidth("100%");
        tbCompanies.setHeight("500px");

        tbPeople.setWidth("100%");
        tbPeople.setHeight("500px");

        // selectable
        tbCompanies.setSelectable(true);
        tbCompanies.setMultiSelect(false);
        tbCompanies.setImmediate(false); // react at once when something is selected


        tbPeople.setSelectable(true);
        tbPeople.setMultiSelect(false);
        tbPeople.setImmediate(true);


        // turn on column reordering and collapsing
        tbCompanies.setColumnReorderingAllowed(true);
        tbCompanies.setColumnCollapsingAllowed(true);

        tbPeople.setColumnReorderingAllowed(true);
        tbPeople.setColumnCollapsingAllowed(true);

        // listen for valueChange, a.k.a 'select' and update the label
        tbCompanies.addListener(new Table.ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {


                value = event.getProperty().getValue().toString();



            }
        });


        /* Suchfeld */
        Label label_search_co = new Label("Suche");
        searchCo = new TextField();
        //suche.setInputPrompt("Suche");
        searchCo.setValue("");
        searchCo.setImmediate(true);


        searchCo.addListener(new TextField.ValueChangeListener() {

            public void valueChange(ValueChangeEvent event) {

                suchbegriff = searchCo.getValue().toString();

                tbCompanies.removeAllItems();

                /* Wenn eine Eingabe im Suchfeld getätigt wurde...*/
                if (suchbegriff.length() != 0) {
                    /*So viele Durchläufe wie die Anzahl der Datensätze*/
                    for (int i = 0; i < firmen.size(); i++) {
                        /*Nimmt den Datensatz aus der ArrayList und wandelt toString*/
                        datensatz = firmen.get(i).toString();

                        /* Wenn der Datensatz die gesuchte Eingabe enthält...(Groß/Kleinschreibung egal)*/
                        if (datensatz.toLowerCase().contains(suchbegriff.toLowerCase())) {

                            /* Container neu befüllen */
                            fillContainerCompanies(i);
                        }
                    }
                    /*
                     * Wenn die Eingabemaske wieder leer ist, wird der
                     * Container wieder mit allen Datensätzen befüllt
                     */
                } else if (suchbegriff.length() == 0) {

                    for (int i = 0; i < firmen.size(); i++) {
                        fillContainerCompanies(i);
                    }
                }
            }
        });

        l1.addComponent(label_search_co);
        l1.addComponent(searchCo);
        l1.addComponent(tbCompanies);




        /* Suchfeld Personen */
        Label label_search_pe = new Label("Suche");
        searchPe = new TextField();
        //suche.setInputPrompt("Suche");
        searchPe.setValue("");
        searchPe.setImmediate(true);


        searchPe.addListener(new TextField.ValueChangeListener() {

            /* Analog zur Firmensuche */
            public void valueChange(ValueChangeEvent event) {

                suchbegriff = searchPe.getValue().toString();

                tbPeople.removeAllItems();

                if (suchbegriff.length() != 0) {
                    for (int i = 0; i < personen.size(); i++) {
                        datensatz = personen.get(i).toString();
                        if (datensatz.toLowerCase().contains(suchbegriff.toLowerCase())) {

                            fillContainerPeople(i);
                        }
                    }

                } else if (suchbegriff.length() == 0) {

                    for (int i = 0; i < personen.size(); i++) {
                        fillContainerPeople(i);
                    }
                }
            }
        });

        l2.addComponent(label_search_pe);
        l2.addComponent(searchPe);
        l2.addComponent(tbPeople);
    }

    public void fillContainerCompanies(int j) throws ConversionException, ReadOnlyException {
        /* Container für die Tabelle befüllen */

        //int i = j;

        String firma = firmen.get(j).getFiName();
        long id = firmen.get(j).getFiID();
        Item item = firm_container.addItem(id);
        item.getItemProperty("Firma").setValue(firma);
        item.getItemProperty("Id").setValue(id);


    }

    public void fillContainerPeople(int j) throws ConversionException, ReadOnlyException {
        /* Container für die Tabelle befüllen */

        String name = personen.get(j).getPeNachname();
        String vorname = personen.get(j).getPeVorname();
        String strasse = personen.get(j).getPeAnschriftZeile1();
//            String strasse2 = personen.get(j).getPeAnschriftZeile2();
        String plz = personen.get(j).getPePLZ();
        String ort = personen.get(j).getPeOrt();
        long id = personen.get(j).getPeID();
        Item item = pers_container.addItem(id);
        item.getItemProperty("Name").setValue(name);
        item.getItemProperty("Vorname").setValue(vorname);
        item.getItemProperty("Id").setValue(id);
        item.getItemProperty("Strasse").setValue(strasse);
        item.getItemProperty("PLZ").setValue(plz);
        item.getItemProperty("Ort").setValue(ort);

    }

    public void selectedTabChange(SelectedTabChangeEvent event) {
        TabSheet tabsheet = event.getTabSheet();
        Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
        if (tab != null) {
            mainWindow.showNotification(tab.getCaption());
        }
    }

//    public Container getContainer() {
//        return this.container;
//    }
//    public static IndexedContainer getIndexedContainer() {
//        return firm_container;
//    }
    public void valueChange(ValueChangeEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
